/**
 * 
 */
package com.application.metroscanner;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.application.metroscanner.tools.FileManager;
import com.example.metroscanner.R;

/**
 * @author ElGuay
 * 
 */
public class SubmitRideDialog extends Dialog {
	/* TOOLS */
	Context applicationContext;
	SimpleDateFormat dateFormat;
	
	/* DATA */
	Date startDate;
	Date finishDate;
	long rideTime;
	float rideRating;
	private NumberFormat ratingFormatter;

	/* COMPONENTS */
	RatingBar ratingBar;
	Button submitButton;

	/**
	 * @param context
	 */
	public SubmitRideDialog(Context iContext, Date iStartDate, Date iFinishDate) {
		super(iContext);
		applicationContext = iContext;
		startDate = iStartDate;
		finishDate = iFinishDate;
		dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.FRANCE);
		ratingFormatter = new DecimalFormat("#.#");
	}

	// http://www.coderzheaven.com/2013/02/13/extend-dialog-class-write-its-button-click-activity-interfaces-android/

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.save_ride_dialog);

		final TextView textViewFrom = (TextView) findViewById(R.id.textViewFrom);
		textViewFrom.setText(dateFormat.format(startDate));

		final TextView textViewTo = (TextView) findViewById(R.id.textViewTo);
		textViewTo.setText(dateFormat.format(finishDate));

		final TextView textViewTime = (TextView) findViewById(R.id.textViewTime);
		textViewTime.setText(computeTravelTime());

		submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setEnabled(false);

		addListenerOnRatingBar();
		addListenerOnButton();
	}

	/**
	 * Compute travel time
	 **/

	private String computeTravelTime() {
		String res = "";
		rideTime = finishDate.getTime() - startDate.getTime();
		long diffMinutes = rideTime / (60 * 1000) % 60;
		long diffHours = rideTime / (60 * 60 * 1000) % 24;

		res = diffHours + "h " + diffMinutes + "mn";
		return res;
	}

	/**
	 * Create CSV line
	 **/

	private String CreateCSVLine() {
		// format : dayOfWeek;month;day;hour;minute;time;rating		
		StringBuffer line = new StringBuffer();
		
		//set date infos
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		line.append(calendar.get(Calendar.DAY_OF_WEEK));
		line.append(";");
		line.append(calendar.get(Calendar.MONTH));
		line.append(";");
		line.append(calendar.get(Calendar.DAY_OF_MONTH)); 
		line.append(";");
		line.append(calendar.get(Calendar.HOUR_OF_DAY)); 
		line.append(";");
		line.append(calendar.get(Calendar.MINUTE)); 
		line.append(";");
		
		//set rid infos
		line.append(rideTime); 
		line.append(";");
		line.append(ratingFormatter.format(rideRating));
		
		return line.toString();
	}

	public void addListenerOnRatingBar() {

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);
		// if rating value is changed,
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				rideRating = rating;
				submitButton.setEnabled(true);
			}
		});
	}

	public void addListenerOnButton() {
		// if click on me, then display the current rating value.
		submitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				String csvLine = CreateCSVLine();
				
				Toast.makeText(applicationContext, csvLine, Toast.LENGTH_SHORT).show();
				FileManager manager = FileManager.getInstance(applicationContext);
				manager.AddCSVLine(csvLine);
				dismiss();
			}
		});

	}
}
